package com.lagou.zk;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;


import java.sql.SQLException;

import java.util.Properties;

public class DruidUtils {



    /**
     * 获取连接
     * @param url zk输入的连接url
     * @param username zk输入的账号
     * @param password zk输入的密码
     * @return
     */
    public static DataSource getDataSource(String url,String username,String password){

        try {
            //连接数据库配置信息
            Properties p = new Properties();
            p.clear();
            p.setProperty("driverClassName","com.mysql.jdbc.Driver");
            p.setProperty("url",url);
            p.setProperty("username",username);
            p.setProperty("password",password);
            p.setProperty("initialSize","5");
            p.setProperty("maxActive","10");
            p.setProperty("maxWait","3000");
            //获取连接池
            DataSource dataSource = DruidDataSourceFactory.createDataSource(p);
            return dataSource;

        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



}
